package com.jy.novel.common;

/**
 * Created by jihb on 4/12/16.
 */
public class CommonResult<T> {

    /**
     * 返回码 0-正确
     *
     */
    private int code;
    /**
     * 提示信息
     */
    private String message;
    /**
     * 返回数据
     */
    private T result;
    /**
     * 是否成功
     */
    private boolean success;

    public CommonResult() {

    }

    public CommonResult(int code, String message, T result, boolean success) {
        this.code = code;
        this.message = message;
        this.result = result;
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public static CommonResult returnWrap(String message, int code, Object result){
        return  new CommonResult(code,message,result,code==0);
    }

    public static CommonResult returnDataWrap(Object result){
        return returnWrap("success",0,result);
    }
    public static CommonResult returnBoolWrap(boolean success){
        return returnBoolWrap(success,success?"success":"error");
    }
    public static CommonResult returnBoolWrap(boolean success, String message){
        return returnWrap(message,success?0:-1,null);
    }
    public static CommonResult returnFailure(String message){
        return returnFailure(message,null);
    }
    public static CommonResult returnFailure(String message, Object result){
        return returnWrap(message,-1,result);
    }
}
